<?php

/*
 * by: PhpStorm
 * 2021/7/23
 * 21:40
 * User: jweicao@qq.com
 */

namespace App\Api\Services;


class JsonService
{
    private static $SUCCESSFUL_DEFAULT_MSG = 'ok';

    private static $FAIL_DEFAULT_MSG = 'no';

    public static function result($code,$msg='',$data=[],$count=0)
    {
        return compact('code','msg','data','count');
    }
    public static function successLayui($count=0,$data=[],$msg='')
    {
        if(is_array($count)){
            if(isset($count['data'])) $data=$count['data'];
            if(isset($count['count'])) $count=$count['count'];
        }
        if(false == is_string($msg)){
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result(0,$msg,$data,$count);
    }

    /**
     * 成功返回数据接口
     * @param string $msg
     * @param array $data
     * @param int $status
     * @return array
     */
    public static function success($msg = 'ok',$data=[],$status=200)
    {
        if(false == is_string($msg)){
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result($status,$msg,$data);
    }

    /**
     * @param $status
     * @param $msg
     * @param array $result
     * @return array
     */
    public static function status($status,$msg,$result = [])
    {
        $status = strtoupper($status);
        if(true == is_array($msg)){
            $result = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        return self::result(200,$msg,compact('status','result'));
    }

    /**
     * 失败返回数据接口
     * @param $msg
     * @param array $data
     * @param bool $code
     * @return array
     */
    public static function fail($msg,$data=[],$code = false)
    {
        if(true == is_array($msg)){
            $data = $msg;
            $msg = self::$FAIL_DEFAULT_MSG;
        }
        return self::result($code ? $code : 400,$msg,$data);
    }
}
